﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;
using System.Data;

public partial class IFRS_SEC03_1S : PageBase
{
    #region "---------- Property -----------"

    private RoleGroupUser RoleGroupUser = new RoleGroupUser();
    private Paging paging = new Paging();

    #endregion

    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            this.BindDDL();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
        {

            LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");
            LinkButton lnkEdit = (LinkButton)e.Row.FindControl("lnkEdit");
            Image imgEdit = (Image)e.Row.FindControl("imgEdit");
            Image imgDelete = (Image)e.Row.FindControl("imgDelete");
            DataRowView drv = (e.Row.DataItem as DataRowView);
            if ((bool)drv.Row["IsSystemUse"])
            {
                lnkDelete.Visible = false;
                lnkEdit.Visible = false;
                imgEdit.Visible = false;
                imgDelete.Visible = false;

            }
            else
            {
                lnkDelete.Visible = true;
                lnkEdit.Visible = true;
                imgEdit.Visible = true;
                imgDelete.Visible = true;
             
            }
            //e.Row.SetRowAttribute(drv.Row["RoleGroupID"].ToString());
            e.Row.SetRowAttribute("RoleGroupUserID");
            e.Row.SetLinkAttribute("lnkView", "RoleGroupUserID");
            e.Row.SetLinkAttribute("lnkEdit", "RoleGroupUserID");
            e.Row.SetLinkAttribute("lnkDelete", "RoleGroupUserID");
        }
    }
    protected void gvSearch_Sorting(object sender, GridViewSortEventArgs e)
    {
        paging = Page.LoadViewState(paging, ViewState);

        SortingExpression = e.SortExpression.ToUpper();
        SortingDirection = SortingDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

        paging.Sorting = e.Sorting(SortingExpression, SortingDirection);
        Page.SaveViewState(paging, ViewState);

        GetData(paging.PageIndex);
    }
    protected void lnkDeleteItems_Click(object sender, EventArgs e)
    {

        RoleGroupUser vRoleGroupUser = new RoleGroupUser();
        string strKey = ((LinkButton)sender).Attributes["RoleGroupUserID"];
        vRoleGroupUser.RoleGroupUserID = strKey.AsInt32();
        vRoleGroupUser.SetStandardField();
        int key = new SPR_ROLEGROUPUSER().DELETE(vRoleGroupUser);
        if (key > 0)
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.DeleteCompleted);
            BindSearchData();
        }
        else
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.DeleteFailed);
        }
    }
    protected void lnkEditItems_Click(object sender, EventArgs e)
    {
        //this.SetSearchParamToSessionManager();
        string strKey = ((LinkButton)sender).Attributes["RoleGroupUserID"];
        Response.Redirect(ResolveUrl(string.Format("~/IFRS/SEC03_2E.aspx?&id={0}", strKey)));
    }
    protected void lnkViewItems_Click(object sender, EventArgs e)
    {
        //this.SetSearchParamToSessionManager();
        string strKey = ((LinkButton)sender).Attributes["RoleGroupUserID"];
        Response.Redirect(ResolveUrl(string.Format("~/IFRS/SEC03_4V.aspx?&id={0}", strKey)));
    }
    protected void btnSEARCH_Click(object sender, EventArgs e)
    {
        BindSearchData();
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Index/Index.aspx");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/SEC03_3A.aspx");
    }
    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetData(pageIndex);
    }
    #endregion
    #region "---------- Methods ----------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.SEC03_1S;
    }
    private void BindSearchData()
    {
        this.GetData(1);
    }
    private void BindDDL()
    {
        ddlROLE_GROUP.SetData(DDL_TYPE.ROLEGROUP, "", false, true);
        ddlSTATUS.Items.Insert(0, new ListItem("ทั้งหมด", ""));
        ddlSTATUS.Items.Insert(1, new ListItem("ใช้งาน", "1"));
        ddlSTATUS.Items.Insert(2, new ListItem("ไม่ใช้งาน", "0"));
    }

    private void GetData(int pageIndex)
    {

        paging = Page.LoadViewState(paging, ViewState) ?? new Paging();
        paging.Sorting = paging.Sorting == null ? "" : paging.Sorting;
        paging.PageSize = gvSearch.PageSize;
        paging.PageIndex = pageIndex;


        if (ddlROLE_GROUP.SelectedValue.ToString() != "")
        {
            RoleGroupUser.RoleGroupID = ddlROLE_GROUP.SelectedValue.AsInt32();
        }
        else
        {
            RoleGroupUser.RoleGroupID = (int?)null;
        }
        if (ddlSTATUS.SelectedValue.ToString() != "")
        {
            RoleGroupUser.IsActive = ddlSTATUS.SelectedValue == "1" ? true : false;
        }
        else
        {
            RoleGroupUser.IsActive = (bool?)null;
        }


        int recordCount = 0;
        var spr = new SPR_ROLEGROUPUSER();
        var dt = spr.SEARCH_ALL(RoleGroupUser, paging,txtCostCenter.Text, ref recordCount);

        gvSearch.DataSource = dt;
        gvSearch.DataBind();
        this.PopulatePager(recordCount, pageIndex);

        Page.SaveViewState(paging, ViewState);
    }
    private void PopulatePager(int recordCount, int currentPage)
    {
        // double dblPageCount = (double)((decimal)recordCount / decimal.Parse(ddlPageSize.SelectedValue));
        double dblPageCount = (double)((decimal)recordCount / 10);
        int pageCount = (int)Math.Ceiling(dblPageCount);

        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            pages.Add(new ListItem("First", "1", currentPage > 1));
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }

            pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
        }

        repPager.DataSource = pages;
        repPager.DataBind();
    }
    private string SortingExpression
    {
        get
        {
            return ViewState["SortingExpression"] != null
                ? Convert.ToString(ViewState["SortingExpression"])
                : string.Empty;
        }
        set { ViewState["SortingExpression"] = value; }
    }
    private SortDirection? SortingDirection
    {
        get
        {
            return ViewState["SortDirection"] != null
                ? (SortDirection?)ViewState["SortDirection"]
                : null;
        }
        set { ViewState["SortDirection"] = value; }
    }

    #endregion

}